package cur.channel.server.netty;

import com.alibaba.fastjson.JSON;
import cur.channel.server.model.Message;
import cur.channel.server.model.GroupTaskMsg;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

/**
 * 服务器端启动
 * Created by gantianxing on 2015/12/15.
 */
@Component
public class NettyServiceUtil {
    private static final Log log = LogFactory.getLog(NettyServiceUtil.class);

    public void send(String msg){
        try{
            GroupTaskMsg taskMsg = new GroupTaskMsg();
            taskMsg.setGroupId(111);
            taskMsg.setClientId("211111");
            taskMsg.setMsg(msg);
            NettyClientMap.getRadomChannel().channel.writeAndFlush(JSON.toJSONString(new Message(taskMsg)));

//            ClientInfo jshopClient = NettyServerInfo.getRandomChannl();
//            if(jshopClient!=null){
//                NettyClient nettyClient = jshopClient.getNettyClient();
//                if (nettyClient.channel != null) {
//                    log.info("random send task.controller address:"+nettyClient.channel.remoteAddress());
//                    GroupTaskMsg taskMsg = new GroupTaskMsg();
//                    taskMsg.setGroupId(groupId);
//                    taskMsg.setClientId(clientId);
//                    taskMsg.setMsg(msg);
//                    nettyClient.channel.writeAndFlush(taskMsg);
//                }
//            }else
//            {
//                log.error("没有可用的服务");
//            }

        } catch (Exception e){
            log.error("定时发起task任务失败",e);
        }
    }

}
